package com.example.itensyuan0609.dh;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.example.itensyuan0609.R;
import com.example.itensyuan0609.util.CircleImage;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private Button a;
    private Button b;
    private Button c;
    private Button d;
    private CircleImage ima;
    private Button e;
    AlphaAnimation alphaAnimation;
    ScaleAnimation scaleAnimation;
    RotateAnimation rotateAnimation;
    TranslateAnimation translateAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        a = (Button) findViewById(R.id.a);
        b = (Button) findViewById(R.id.b);
        c = (Button) findViewById(R.id.c);
        d = (Button) findViewById(R.id.d);
        ima = (CircleImage) findViewById(R.id.ima);

        a.setOnClickListener(this);
        b.setOnClickListener(this);
        c.setOnClickListener(this);
        d.setOnClickListener(this);
        e = (Button) findViewById(R.id.e);
        e.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        into();
        switch (v.getId()) {
            case R.id.a:
                ima.startAnimation(alphaAnimation);
                break;
            case R.id.b:
                ima.startAnimation(scaleAnimation);
                break;
            case R.id.c:
                ima.startAnimation(rotateAnimation);
                break;
            case R.id.d:
                ima.startAnimation(translateAnimation);
                break;
            case R.id.e:
                AnimationSet animationSet = new AnimationSet(false);
                animationSet.addAnimation(alphaAnimation);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(rotateAnimation);
                animationSet.addAnimation(translateAnimation);
                ima.startAnimation(animationSet);
                break;
        }
    }

    public void into() {
        //透明度
        alphaAnimation = new AlphaAnimation(1.0f, 0);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setRepeatCount(1);

        //缩放
        scaleAnimation = new ScaleAnimation(Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 4,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        scaleAnimation.setRepeatCount(1);

        //旋转
        rotateAnimation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f
                , Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setRepeatMode(Animation.REVERSE);
        rotateAnimation.setRepeatCount(5);

        //位移
        translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -0.3f,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, -1);
        translateAnimation.setDuration(3000);
        translateAnimation.setRepeatMode(Animation.REVERSE);
        translateAnimation.setRepeatCount(1);
    }
}
